<?php

/*
 * @author Ebrain Cedeno <ebraincedeno@gmail.com>
 */


class RolController extends Controller{
    
    public function actionNew(){
        
        VeCms::AccessValidate(array("admin"=>1));
        
        $rol = new rol();
        if (isset($_POST['guardar'])){
            $rol->setAttributes($_POST);
            if ($rol->validate()){
                if ($rol->save()){
                    TForm::addMessage("registrado", "El registro se realizo correctamente");
                    $this->redirec(VeCms::createUrl("./grid"),5);
                }
            }
        }
        
        $this->link_back = new TLink("Regresar");
            $js_link = "parent.history.back(); 
                        return false; 
                        ";
        $this->link_back->onClick = $js_link;
        $this->link_back->OptionsAditional['style'][] = "color:#A9BCF5";
        $this->link_back->OptionsAditional['style'][] = "font-weight:bold";
        
        $this->form = new TForm();
        $this->form->Method = "POST";
        $this->form->Action = VeCms::createUrl("./new");
        
        $this->description_label = new TLabel("Descripcion:");
        $this->description_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        
        $this->description_text = new TTextView();
        $this->description_text->Name = "description";
        $this->description_text->Id = $this->description_text->Name;
        $this->description_text->Cols = "10";
        $this->description_text->Rows = "10";
        $this->description_text->Width = 250;
        $this->description_text->Height = 100;
        $this->description_text->Text = $rol->description;
        $this->description_text->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->admin_label = new TLabel("Es Administrador?");
        $this->admin_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        
        $this->admin_checkbox = new TCheckBox();
        $this->admin_checkbox->Name = "admin";
        $this->admin_checkbox->Checked = false; 
        
        $this->button_cancel = new TButton("Cancelar");
        $this->button_cancel->Type = "reset";
        $this->button_cancel->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
        $this->button_guardar = new TButton("Guardar");
        $this->button_guardar->Name = "guardar";
        $this->button_guardar->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));

        
    }
    
    public function actionGrid(){
        
        VeCms::AccessValidate(array("admin"=>1));
        
        if (isset($_GET['del'])){
            $this->rol_del = true;
            $this->validate = new TForm();
            TForm::addMessage('eliminado', "El Rol se elimino correctamente");
        }
        else{
            $this->rol_del = false;
        }
        
        //botton para agregar nuevos roles
        
        $this->button_new_rol = new TButton("Nuevo Rol");
        
        $js_rol = '$(location).attr("href","./new");';
        
        $this->button_new_rol->onClick = $js_rol;
        $this->button_new_rol->OptionsAditional = array('style'=>array('font-weight:bold',
                                                                        'cursor:pointer',
                                                                        'border:1px solid gray',
                                                                        'background-image:url('.VeCms::getPathImage().VeCmsEnum::ICON_ADD.')',
                                                                        'background-position:left',
                                                                        'background-repeat: no-repeat',
                                                                        'background-position-x:5px',
                                                                        'text-align: right'));
        
        //creamos el grid de los roles en el sistema
        
        $offset = (isset($_GET['offset']))?$_GET['offset']:0;
        $rol = new rol();
        $rol->Offset = $offset;
        $rol->Limit = 10;
        $estosRoles = $rol->findAll('active=:active',array(':active'=>1));
        
        $this->grid_rol = new TTable();
        $this->grid_rol->Id = "grid-user";
        $this->grid_rol->Columns = 6;
        $this->grid_rol->Rows = $rol->Limit;
        $this->grid_rol->Width = '100%';
        $this->grid_rol->Headers = array('Nro','Descripcion','Administrador','','','');
        $this->grid_rol->WidthCell[0] = 25;
        $this->grid_rol->WidthCell[3] = 25;
        $this->grid_rol->WidthCell[4] = 25;
        $this->grid_rol->WidthCell[5] = 25;
        $this->grid_rol->RowsAlign[0] = 'center';
        
        
        $count = 1;
        foreach ($estosRoles as $key=>$value){
            
            //cargamos la accion para vista previa
            
            $view = new TImage();
            $view->Src = VeCms::getPathImage().  VeCmsEnum::ICON_VIEW;
            $js_view = '$(location).attr("href","./view?id_rol='.$value->id_rol.'")';
            $view->onClick = $js_view;
            $view->OptionsAditional['style'][] = 'cursor:pointer';
            
            //cargamos la accion para editar rol
            
            $edit = new TImage();
            $edit->Src = VeCms::getPathImage(). VeCmsEnum::ICON_EDIT;
            $js_edit = '$(location).attr("href","./update?id_rol='.$value->id_rol.'");';
            $edit->onClick = $js_edit;
            $edit->OptionsAditional['style'][] = 'cursor:pointer';
            
            //cargamos la accion para eliminar rol
            
            $delete = new TImage();
            $delete->Src = VeCms::getPathImage(). VeCmsEnum::ICON_DELETE;
            
            $js_delete = 'if (confirm(\'Esta seguro que desea eliminar este rol?\')){
                                $(location).attr("href","delete?id_rol='.$value->id_rol.'");
                            }';
            
            $delete->onClick = $js_delete;
            $delete->OptionsAditional['style'][] = 'cursor:pointer';
            
            $IsAdmin = ($value->admin == 1)?'Si':'No';
            $this->grid_rol->Items[]= array($count,$value->description,$IsAdmin,$view->show(),$edit->show(),$delete->show());
            $count++;
        }
        
        $this->grid_rol->Pagination = new TPagination(array('offset'=>$offset,'limit'=>$rol->Limit,'total'=>  rol::model()->Count('active=:active',array(':active'=>1))));
        
    }
    
    public function actionView(){
        
        VeCms::AccessValidate(array("admin"=>1));
        
        if (isset($_GET['id_rol'])){
            $rol = rol::model()->find("id_rol=:id_rol",array(':id_rol'=>$_GET['id_rol']));
            
            $this->link_back = new TLink("Regresar");
            $js_link = "parent.history.back(); 
                        return false; 
                        ";
            $this->link_back->onClick = $js_link;
            $this->link_back->OptionsAditional['style'][] = "color:#A9BCF5";
            $this->link_back->OptionsAditional['style'][] = "font-weight:bold";
            
            $this->description_label = new TLabel("Descripcion:");
            $this->description_label->OptionsAditional['style'][] = 'font-weight:bold';
            $this->description_value = new TLabel($rol->description);
            
            $this->admin_label = new TLabel("Administrador");
            $this->admin_label->OptionsAditional['style'][] = 'font-weight:bold';
            $this->admin_value = new TLabel(($rol->admin == 1)?'Si':'No');
            
        }
        else{
            throw new Exception("No se encontro el rol solicitado");
        }
    }
    
    public function actionUpdate(){
        
        VeCms::AccessValidate(array("admin"=>1));
        
        $rolclass = new rol();
        if (isset($_POST['actualizar'])){
            $rolclass->setAttributes($_POST);
            if ($rolclass->validate()){
                if ($rolclass->update()){
                    TForm::addMessage("registrado", "El registro se actualizo correctamente");
                    $this->redirec(VeCms::createUrl("./grid"),5);
                }
            }
        }
        
        $rol = $rolclass->find("id_rol=:id_rol",array(':id_rol'=>$_GET['id_rol']));
        
        $this->link_back = new TLink("Regresar");
            $js_link = "parent.history.back(); 
                        return false; 
                        ";
        $this->link_back->onClick = $js_link;
        $this->link_back->OptionsAditional['style'][] = "color:#A9BCF5";
        $this->link_back->OptionsAditional['style'][] = "font-weight:bold";
        
        
        $this->form = new TForm();
        $this->form->Method = "POST";
        $this->form->Action = VeCms::createUrl("./update?id_rol=".$_GET['id_rol']);
        
        $this->description_label = new TLabel("Descripcion:");
        $this->description_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        
        $this->description_text = new TTextView();
        $this->description_text->Name = "description";
        $this->description_text->Id = $this->description_text->Name;
        $this->description_text->Cols = "10";
        $this->description_text->Rows = "10";
        $this->description_text->Width = 250;
        $this->description_text->Height = 100;
        $this->description_text->Text = $rol->description;
        $this->description_text->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->admin_label = new TLabel("Es Administrador?");
        $this->admin_label->OptionsAditional = array('style'=>array('font-weight:bold'));
        
        $this->admin_checkbox = new TCheckBox();
        $this->admin_checkbox->Name = "admin";
        $this->admin_checkbox->Checked = false; 
      
        $this->id_rol_entry = new TEdit();
        $this->id_rol_entry->Name = "id_rol";
        $this->id_rol_entry->Id = $this->id_rol_entry->Name;
        $this->id_rol_entry->Text = $_GET['id_rol'];
        $this->id_rol_entry->IsHidden = true;
        
        $this->button_cancel = new TButton("Cancelar");
        $this->button_cancel->Type = "reset";
        $this->button_cancel->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
        $this->button_guardar = new TButton("actualizar");
        $this->button_guardar->Name = "actualizar";
        $this->button_guardar->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));

    }
    
    public function actionDelete(){
        
        VeCms::AccessValidate(array("admin"=>1));
        
        if (isset($_GET['id_rol'])){
            $rol = rol::model()->find('id_rol = :id_rol',array(':id_rol'=>$_GET['id_rol']));
            $rol->active = 0;
            if ($rol->update()){
                $this->redirec('./grid',0,array("del"=>true));
            }
            else{
                throw new Exception("Error al intentar eliminar el rol con el id ".$_GET['id_rol']);
            }
            
        }
    }
    
}
?>
